package com.dure.hotmemory.controllers;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.ProgressBar;

import com.dure.commonlib.util.ApplicationUtils;
import com.dure.commonlib.util.AssetsUtil;
import com.dure.commonlib.util.FileUtils;
import com.dure.commonlib.util.NetworkUtil;
import com.dure.commonlib.util.PreferencesUtils;
import com.dure.commonlib.util.StreamUtil;
import com.dure.commonlib.util.ToastUtils;

import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.AD;
import com.dure.hotmemory.bean.AppUpdate;
import com.dure.hotmemory.bean.Bean;
import com.dure.hotmemory.bean.Code;
import com.dure.hotmemory.bean.CodeDao;
import com.dure.hotmemory.bean.Planet;
import com.dure.hotmemory.config.Config;
import com.dure.hotmemory.config.Constants;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.scenes.HomeScene;
import com.dure.hotmemory.scenes.MainScene;
import com.dure.hotmemory.scenes.TrainingInfoScene;
import com.dure.hotmemory.server.UpdateService;
import com.dure.hotmemory.utils.DialogUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.socks.library.KLog;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dure on 2017/4/17.
 */

public class MainController extends Controller {


    MainScene mainScene;

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    activity.moveToNewScene(new HomeScene(baseSurfaceView));
                    break;
                case 1:
//                    if (!PreferencesUtils.getBoolean(activity, "login_success")) {
//                        ToastUtils.show(activity, "请先登录");
//                    } else{
//                        TrainingStatisticsScene trainingStatisticsScene = new TrainingStatisticsScene(baseSurfaceView);
//                        trainingStatisticsScene.setCurrentPlanet(mainScene.getCurrentPlanet());
//                        activity.moveToNewScene(trainingStatisticsScene);
//                    }

                    break;
                case 2:
                    handlePlanetClick(mainScene.getClickedPlanet());
                    mainScene.setClickedPlanet(null);
                    break;
            }
        }
    };

    public MainController(MainScene mainScene) {
        super(mainScene);
        this.mainScene = mainScene;
    }

    public Planet getAllPlanets() {

        InputStream is = AssetsUtil.getAssets(activity, "universe.json");
        Gson gson = new Gson();
        String universeJsonStr = StreamUtil.inputStream2String(is);
        Planet universe = gson.fromJson(universeJsonStr, Planet.class);

        return universe;
    }


    public void checkAdStatus() {
        if (!PreferencesUtils.getBoolean(activity, "paied")) {
            if (NetworkUtil.isNetworkAvailable(activity)) {
                HashMap<String, String> paramMap = new HashMap<>();
                paramMap.put("platform", ApplicationUtils.getApplicationMetaData(activity, "UMENG_CHANNEL"));
                requestModel.doGet(Config.HOST + Config.AD_STATUS, paramMap, AD.class);
            }
        }
    }


    public void checkUpdate() {
        KLog.d("lesleixz", "check update");
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("app_version", ApplicationUtils.getAppVersion(activity));
        paramMap.put("os_type", "android");

        requestModel.doPost(Config.HOST + Config.CHECK_VERSION, paramMap, AppUpdate.class);
    }


    @Override
    public void handleRequestSuccess(Bean bean) {
        if (bean instanceof AppUpdate){
            final AppUpdate appUpdate = (AppUpdate) bean;
            if (appUpdate.is_old) {// 显示有新版本对话框
                DialogUtil.showUpdateDialog(activity, new ConfirmListener() {
                    @Override
                    public void confirmed(Bundle bundle) {
                        goToDownload(appUpdate);
                    }
                });
            } else {// 检查本地目录是否存有旧的apk,有则将其删除
                if (!TextUtils.isEmpty(Constants.download_path))
                    FileUtils.deleteFiles(Constants.download_path);
            }

            checkAdStatus();
        }else if (bean instanceof AD) {
            AD ad = (AD) bean;
            PreferencesUtils.putBoolean(activity, "is_removed", ad.is_removed);
            PreferencesUtils.putString(activity, "ad_type", ad.ad_type);
            activity.showAd();
        }
    }


    public void goToDownload(AppUpdate appUpdate) {
        Intent intent = new Intent(activity, UpdateService.class);
        intent.putExtra("url", appUpdate.url);
        intent.putExtra("img_id", R.mipmap.ic_launcher);
        intent.putExtra("name", R.string.app_name);
        activity.startService(intent);
    }



    public void handlePlanetClick(Planet planet) {
        if (planet == null)
            return;

        if (TextUtils.equals(planet.planet, "自定计划")) {
            goToMyPlan();
        } else if (TextUtils.equals(planet.parent_planet.planet, "关于速读")) {
//            WebScene webScene = new WebScene(baseSurfaceView);
//            webScene.loadData("file:///android_asset/html/" + planet.clazz + ".html");
//            activity.moveToNewScene(webScene);
        }
//        else if (TextUtils.equals(planet.parent_planet.planet, "定桩训练")){
//            BianMaScene bianMaScene = new BianMaScene(baseSurfaceView);
//            activity.moveToNewScene(bianMaScene);
//        }
        else {
            if (!planet.disable) {
                TrainingInfoScene trainingInfoScene = new TrainingInfoScene(baseSurfaceView);
                trainingInfoScene.setCurrentPlanet(planet);

                activity.moveToNewScene(trainingInfoScene);
            }
//            goToTrainingInfo(planet);
        }
    }

    private void goToTrainingInfo(Planet planet) {
//        TrainingInfoScene trainingInfoScene = new TrainingInfoScene(baseSurfaceView);
//        activity.moveToNewScene(trainingInfoScene);
//        trainingInfoScene.setCurrentPlanet(planet);


//        JDTXScene jdtxScene = new JDTXScene(baseSurfaceView);
//        JDCYScene jdcyScene = new JDCYScene(baseSurfaceView);
//        JDSZYScene szjyScene = new JDSZYScene(baseSurfaceView);
//        BMXLScene bmxlScene = new BMXLScene(baseSurfaceView);

    }

    private void goToMyPlan() {
        if (!PreferencesUtils.getBoolean(activity, "login_success", false)) {
            ToastUtils.show(activity, "请先登录");
        } else {// 切换到计划界面
//            PlanScene planScene = new PlanScene(baseSurfaceView);
//            activity.moveToNewScene(planScene);
        }
    }






}
